package cn.sise.edu.crm.web.controller;

import cn.sise.edu.crm.domain.Employee;
import cn.sise.edu.crm.domain.JsonResult;
import cn.sise.edu.crm.domain.SystemDictionary;
import cn.sise.edu.crm.domain.TraceCustomerHistry;
import cn.sise.edu.crm.query.SystenDictionaryItemQueryObject;
import cn.sise.edu.crm.service.ITraceCustomerHistryService;
import cn.sise.edu.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by chen on 2019/11/3.
 */
@Controller
@RequestMapping("/traceCustomerHistry")
public class TraceCustomerHistryController {
    @Autowired
    private ITraceCustomerHistryService traceCustomerHistryService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"字典明细页面", "traceCustomerHistry:list"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") SystenDictionaryItemQueryObject qo) {
       // int i =1/0;
        PageInfo<TraceCustomerHistry> pageInfo = traceCustomerHistryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();
        model.addAttribute("systemDictionaries",systemDictionaries);
        return "traceCustomerHistry/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"字典明细增加或更新", "traceCustomerHistry:saveOrUpdate"},logical = Logical.OR)
    public JsonResult saveOrUpdate(TraceCustomerHistry traceCustomerHistry){
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        try {
            if(traceCustomerHistry.getId() != null){
                traceCustomerHistryService.update(traceCustomerHistry);
            }else{
                traceCustomerHistry.setInputUser(employee);
                traceCustomerHistry.setInputTime(new Date());
                traceCustomerHistryService.insert(traceCustomerHistry);
            }
        }catch (Exception e){
           return new JsonResult("保存或者更新失败",false);
        }

        return  new JsonResult("保存或者更新成功",true);
}
}
